import biology.*;
import site.*;
import tool.*;

import java.util.Scanner;

public class Game {
    private Site currentSite;
    private personMonk currentPerson;

    public Game() {
        createRooms();
    }

    //制造房间，初始化房间出口，制造怪物，分配怪物
    private void createRooms() {
        //制造房间
        ordinarySquare square = new ordinarySquare();                                       //城堡广场
        ordinaryToolMagazine toolMagazine = new ordinaryToolMagazine();                     //武器库
        battlegroundPub pub = new battlegroundPub();                                        //酒吧
        battlegroundUndergroundCity undergroundCity = new battlegroundUndergroundCity();    //地下城
        battlegroundBeastCity beastCity = new battlegroundBeastCity();                      //野兽城

        //	初始化房间出口
        square.setExits(toolMagazine, pub, undergroundCity, beastCity);
        toolMagazine.setExits(null,null,square,null);
        pub.setExits(null, null, null, square);
        undergroundCity.setExits(square, null, null, null);
        beastCity.setExits(null,square, null, null);

        //	从城堡广场开始
        currentSite = square;

        //制造怪物
        monsterAssassin assassin = new monsterAssassin();               //刺客
        monsterSkeleton skeleton = new monsterSkeleton();               //骷髅
        monsterVampire vampire = new monsterVampire();                  //吸血鬼
        monsterWerewolf werewolf = new monsterWerewolf();               //狼人
        monsterZombie zombie = new monsterZombie();                     //僵尸
        monsterApe ape = new monsterApe();                              //猿猴兽

        //分配怪物
        pub.monsterArrive(assassin, vampire);
        undergroundCity.monsterArrive(skeleton, zombie);
        beastCity.monsterArrive(werewolf, ape);

        //制造武器
        weaponUnarmed unarmed = new weaponUnarmed();
        weaponLaserGun laserGun = new weaponLaserGun();
        weaponMissileGun missileGun = new weaponMissileGun();
        weaponStoneHammer stoneHammer = new weaponStoneHammer();
        replenishmentLifePotion lifePotion = new replenishmentLifePotion();

        //分配武器
        toolMagazine.setTool(stoneHammer, lifePotion);
        pub.setTool(laserGun, lifePotion);
        undergroundCity.setTool(missileGun, lifePotion);

        currentPerson = new personMonk(unarmed);
    }

    //欢迎消息
    public void printWelcome() {
        System.out.println();
        System.out.println("欢迎来到<--和尚与打怪-->");
        System.out.println("剧情设定穿越时空限制");
        System.out.println("-------------------------------------------------------------");
        System.out.println("　 　 人\t\t从前有座山，山里有座庙。");
        System.out.println("　 （＿＿）\t\t庙里的小和尚[王铁柱]，还在睡大觉的时候\t\t*<(¦Q[▓▓  呼呼呼。。。。");
        System.out.println("　（＿＿＿）\t\t被他师傅叫醒，他师傅叫他下山去化缘\t\t\t(～￣▽￣)→))(´ฅω•ฅ｀)o");
        System.out.println(" （＿＿＿＿）\t半梦半醒的他，想了想自己没有着落的早饭，于是听师傅的话，下山化缘去了");
        System.out.println("下山之后，他发现山下的世界，兵荒马乱，没有人给他香火钱，于是他屡屡碰壁，他十分伤心沮丧，");
        System.out.println("他走着走着来到了城堡广场，忽然");
        System.out.println("他看见了悬赏令，才明白缘由\t\t┌ ○ ┐");
        System.out.println("原来公主被[野兽城]绑架了　\t\t│悬 | ﾊ,,ﾊ ");
        System.out.println("[工具库]只提供[大石锤]　\t\t\t│赏 | ﾟωﾟ )");
        System.out.println("需要先去[酒吧]获取[激光枪]\t\t│令 | ");
        System.out.println("再去[地下城]获取[导弹炮] \t\t└ ○ ┘ ");
        System.out.println("最后去[野兽城]救回公主\t\t\t[城堡广场]->[工具库]->[酒吧]->[地下城]->[野兽城]");
        System.out.println("于是，[王铁柱]打算重拾屠刀，打败怪物，救回公主，赢得赏钱。只为庙里添加香火钱。");
        System.out.println("如果需要帮助，请输入 'help' ");
        System.out.println("-------------------------------------------------------------");
        System.out.println("现在你在" + currentSite.getName());
        printExit();
    }
    //帮助消息
    public void printHelp() {
        System.out.println("迷路了吗？你可以做的命令有：go bye help take attack");
        System.out.println("请求帮助：help");
        System.out.println("关闭游戏：bye");
        System.out.println("移动房间：go +方向\t\t\t如：go east");
        System.out.println("更换武器：take +武器名字\t\t如：take 激光枪");
        System.out.println("攻击怪物：attack +怪物名字\t如：attack 刺客");
        System.out.println("-------------------------------------------------------------");
    }
    //更换地点
    public void goSite(String direction) {
        if (currentSite.getJudgeWhetherToWin() == 0){
            System.out.println("你还没有击杀完所有的怪物，不能离开房间");
        } else {
            Site nextSite = null;
            if (direction.equals("north")) {
                nextSite = currentSite.getNorthExit();
            }
            if (direction.equals("east")) {
                nextSite = currentSite.getEastExit();
            }
            if (direction.equals("south")) {
                nextSite = currentSite.getSouthExit();
            }
            if (direction.equals("west")) {
                nextSite = currentSite.getWestExit();
            }

            if (nextSite == null) {
                System.out.println("那里没有门！");
            } else {
                currentSite = nextSite;
                System.out.println("你在" + currentSite.getName());
                if(ordinarySquare.class.isInstance(currentSite)){
                    printVictory();
                }
                printMonster();
                if (currentSite.getJudgeWhetherToWin() == 1) {
                    printObtain();
                }
            }
        }
    }
    //攻击对象
    public void attack(String monsterObject) {
        if(monsterObject.equals(currentSite.getMonsterFirst().getName()) ) {
            System.out.println("[" + currentPerson.getName() + "]使用[" + currentPerson.getCarryingWeapons().getName() + "]伤害了["
                    + currentSite.getMonsterFirst().getName() + "]" + currentPerson.getCarryingWeapons().getHit() + "点血");
            currentSite.getMonsterFirst().setHealthPoint(currentSite.getMonsterFirst().getHealthPoint() - currentPerson.getCarryingWeapons().getHit());
            if(currentSite.getMonsterSecond().getHealthPoint() != 0){
                System.out.println("同时[" + currentSite.getMonsterSecond().getName() + "]发动技能[" + currentSite.getMonsterSecond().getAction() + "]伤害了["
                        + currentPerson.getName() + "]" + currentSite.getMonsterSecond().getHit() + "点血");
                currentPerson.setHealthPoint(currentPerson.getHealthPoint() - currentSite.getMonsterFirst().getHit());
            }
        } else if(monsterObject.equals(currentSite.getMonsterSecond().getName())) {
            System.out.println("[" + currentPerson.getName() + "]使用[" + currentPerson.getCarryingWeapons().getName()
                    + "]伤害了[" + currentSite.getMonsterSecond().getName() + "]" + currentPerson.getCarryingWeapons().getHit() + "点血");
            currentSite.getMonsterSecond().setHealthPoint(currentSite.getMonsterSecond().getHealthPoint() - currentPerson.getCarryingWeapons().getHit());
            if(currentSite.getMonsterFirst().getHealthPoint()!=0){
                System.out.println("同时[" + currentSite.getMonsterFirst().getName() + "]发动技能[" + currentSite.getMonsterFirst().getAction()
                        + "]伤害了[" + currentPerson.getName() + "]" + currentSite.getMonsterFirst().getHit() + "点血");
                currentPerson.setHealthPoint(currentPerson.getHealthPoint() - currentSite.getMonsterSecond().getHit());
            }
        } else {
            System.out.println("你的攻击落空了！注意瞄准对象再攻击！");
        }
        printDisplay();
    }
    //重新开始
    public void restart() {
        createRooms();
        System.out.println("现在你在" + currentSite.getName());
        printExit();
    }

    //显示出口
    public void printExit() {
        System.out.print("出口有：");
        if(currentSite.getNorthExit() != null)
            System.out.print("\t   north:"+currentSite.getNorthExit().getName());
        if(currentSite.getEastExit() != null)
            System.out.print("   east:"+currentSite.getEastExit().getName());
        if(currentSite.getSouthExit() != null)
            System.out.print("   south:"+currentSite.getSouthExit().getName());
        if(currentSite.getWestExit() != null)
            System.out.print("   west:"+currentSite.getWestExit().getName());
        System.out.println();
    }
    //显示怪兽
    public void printMonster() {
        if (currentSite.getMonsterFirst() != null) {
            System.out.print("怪兽有：");
            System.out.println("\t   [" + currentSite.getMonsterFirst().getName() + "]\t\t的技能是["+ currentSite.getMonsterFirst().getAction()+ "]\t可造成伤害  " + currentSite.getMonsterFirst().getHit() + "\t。目前，他的血量是  " + currentSite.getMonsterFirst().getHealthPoint()+"  。");
        }
        if (currentSite.getMonsterSecond() != null) {
            System.out.print("\t\t\t   [" + currentSite.getMonsterSecond().getName() + "]\t\t的技能是["+ currentSite.getMonsterSecond().getAction()+ "]\t可造成伤害  " + currentSite.getMonsterSecond().getHit() + "\t。目前，他的血量是  " + currentSite.getMonsterSecond().getHealthPoint()+"  。");
            System.out.println();
        }
    }
    //显示工具
    public void printTool() {
        if(currentSite.getObtainWeapon() != null) {
            System.out.print("武器有：");
            System.out.println("\t   " + currentSite.getObtainWeapon().getName());
        }
        if(currentSite.getObtainReplenishment() != null){
            System.out.print("补给有：");
            System.out.println("\t   "+currentSite.getObtainReplenishment().getName());
        }
    }

    //显示小关是否被打败的消息
    public void printDisplay() {
        if((currentSite.getMonsterFirst().getHealthPoint()!=0 || currentSite.getMonsterSecond().getHealthPoint()!=0 ) && currentPerson.getHealthPoint()>0) {
            System.out.println("------------------------------");
            System.out.println("[" + currentPerson.getName() + "]\t剩余血量  " + currentPerson.getHealthPoint()+"，正在使用[" + currentPerson.getCarryingWeapons().getName() + "]进行攻击。");
            System.out.println("[" + currentSite.getMonsterFirst().getName() + "]\t\t剩余血量  " + currentSite.getMonsterFirst().getHealthPoint()+"。");
            System.out.println("[" + currentSite.getMonsterSecond().getName() + "]\t剩余血量  " + currentSite.getMonsterSecond().getHealthPoint()+"。");
            System.out.println("------------------------------");
        } else if(currentPerson.getHealthPoint()>0) {
            System.out.println("------------------------------");
            System.out.println("在["+currentSite.getName()+"]你完胜比赛");
            System.out.println("------------------------------");
            currentSite.setJudgeWhetherToWin(1);
            currentPerson.setFinalJudgeWhetherToWin(currentPerson.getFinalJudgeWhetherToWin()+1);
            printObtain();
        } else if(currentPerson.getHealthPoint()<=0){
            System.out.println("------------------------------");
            System.out.println("在"+currentSite.getName()+"你被打败了!，是否重新开始");
            System.out.println("------------------------------");
        }
    }
    //小关胜利之后的显示
    public  void printObtain() {
        printExit();
        printTool();
    }

    //最终胜利
    public  void printVictory() {
        if(currentPerson.getFinalJudgeWhetherToWin()==3) {
            System.out.println("恭喜你取得最终胜利");
        }
    }

    //携带武器
    public void take(String toolObject) {
        weapon nextWeapon = null;
        if(toolObject.equals(currentSite.getObtainWeapon().getName())) {
            nextWeapon = currentSite.getObtainWeapon();
            currentPerson.setCarryingWeapons(currentSite.getObtainWeapon());
        }
        if(toolObject.equals(currentSite.getObtainReplenishment().getName())) {
            nextWeapon = currentPerson.getCarryingWeapons();
            currentPerson.setHealthPoint(currentPerson.getHealthPoint()+currentSite.getObtainReplenishment().getCure());
            System.out.println("[" + currentPerson.getName() + "]剩余血量" + currentPerson.getHealthPoint());
        }
        if (nextWeapon == null) {
            System.out.println("["+currentSite.getName()+"]暂时没有你想要的武器["+toolObject+"]");
        }
        else {
            System.out.println("["+currentPerson.getName()+"]现在携带的武器是["+currentPerson.getCarryingWeapons().getName()+"]");
        }
    }
}
